import json
import time
import traceback

import requests

createGjjTaskUrl = 'http://localhost:4821/gjjServices/createGjjTask'
queryGjjCodeUrl = 'http://localhost:4821/gjjServices/queryGjjCode'
queryGjjUrl = 'http://localhost:4821/gjjServices/queryGjj'
getGjjDataUrl = 'http://localhost:4821/gjjServices/getGjjData';
callbackUrl = 'http://www.baidu.com';


def createGjjTask(cityId):
    print('创建公积金任务')
    createGjj = requests.get(createGjjTaskUrl + "?cityId=" + str(cityId));
    print(createGjj.text)
    result = createGjj.text
    # 解析公积金任务ID
    print('解析公积金任务ID')
    jsonResult = json.loads(result);
    print(result)
    code = jsonResult['code'];
    if code == 0:
        taskId = jsonResult['data']['taskId'];
    else:
        taskId = -1;
        print(jsonResult['msg'])
    return taskId


def gjjYzm(taskId):
    print('获取验证码')
    yzm = requests.get(queryGjjCodeUrl + '?taskId=' + str(taskId));
    print(yzm.text)
    jsonResult1 = json.loads(yzm.text);
    code = jsonResult1['code'];
    if code == 0:
        img = jsonResult1['data']['img'];
    else:
        img = "";
    print(img)
    return img


def gjjQuery(taskId, callbackUrl, inputJsonData):
    # 查询公积金
    print('查询公积金')
    data = {
        'taskId': taskId,
        'callbackUrl': callbackUrl,
        'data': inputJsonData,
    }
    gjj = requests.post(queryGjjUrl, data);
    jsonResult1 = json.loads(gjj.text);
    print(gjj.text)
    code = jsonResult1['code'];
    if code == 0:
        return True;
    else:
        print("查询公积金失败" + jsonResult1['msg'])
        return False;


def gjjData(taskId):
    # 查询公积金结果
    print('查询公积金结果')
    gjjData = requests.get(getGjjDataUrl + "?taskId=" + str(taskId))
    print(gjjData.text)

    jsonResult1 = json.loads(gjjData.text);
    code = jsonResult1['code'];
    data = jsonResult1['data']['data'];
    return (code, data);


# headers = {'Content-Type': 'application/json'}
# , headers = headers,timeout= 50
if __name__ == "__main__":
    try:
        # cityId=3杭州
        taskId = createGjjTask(3);
        if taskId > 0:
            yzm = gjjYzm(taskId);
            inputJsonStr = '{"customerNumber":1111111,"password":123456}';
            completeFlag = False;
            while (True):
                gjjQueryResult = gjjQuery(taskId, callbackUrl, inputJsonStr)
                if gjjQueryResult == True:
                    time.sleep(1);
                    while True:
                        (code, data) = gjjData(taskId)
                        if code == 0:
                            print("公积金查询结果：" + data)
                            completeFlag = True;
                            break;
                        elif code == 1:
                            jsonResult1 = json.loads(data);
                            print(jsonResult1)
                            inputNewArr = {};
                            for i in jsonResult1:
                                key = i['name'];
                                type = i['type'];###igonal
                                inputNewArr[key] = "abc";
                            inputJsonStr = json.dumps(inputNewArr)
                            completeFlag = False;
                            break;
                        elif code == 105:
                            print("任务正在执行中，请稍后")
                            time.sleep(1)
                            continue;
                        else:
                            print("任务执行失败")
                            completeFlag = True;
                            break;
                    if (completeFlag):  ##任务完成
                        break;
                else:
                    print("查询公积金失败")
        else:
            print("创建公积金任务失败")
    except:
        print("1")
        exstr = traceback.format_exc()
        print(exstr)
